%% 清空环境
clc
clear
close all

for num=1:20    %进行20次实验测试
    runspped=1715265866.21;
    lab=["vgg11","vgg13","vgg16","vgg19","googlenet","darknet53","fuse"];
    file_name="测试结果"+string(num);
    mkdir(file_name)
    for i=1:6
        
        X=xlsread("./params.xlsx",i);                   %读取的数据(输入修改）
        task_num=size(X(:,9),1);
        execution_time(1:task_num,1)=(X(:,9)/sum(X(:,9)))*1;     %云服务器1执行时间
        execution_time(1:task_num,2)=(X(:,9)/sum(X(:,9)))*1.3;   %边缘服务器2执行时间
        execution_time(1:task_num,3)=(X(:,9)/sum(X(:,9)))*1.4;     %边缘服务器3执行时间
        execution_time(1:task_num,4)=(X(:,9)/sum(X(:,9)))*2;     %本地终端执行时间
        %execution_time(:,:)=execution_time(:,:).*X(1,10);
        execution_time(:,:)=execution_time(:,:).*sum(X(:,9))./(4*runspped*0.001);
        narvs = size(X(:,9),1)+7;       %目标函数的自变量个数
        task_out=X(:,8);
        task_in=X(1,4);
        task=xlsread("./task.xlsx",i);
        floot_num(i)=sum(X(:,9));
        %% 初始化设置
        if narvs<30
            MaxNum = 300;                          % 粒子最大迭代次数
        else
            MaxNum=narvs*10;
        end
        
        %% 不卸载LOPSO
        narvs=task_num+7;
        x=zeros(1, narvs);         % 粒子所在的位置
        LO=object_value(x,task,execution_time,task_num,task_out,task_in);
        disp("不卸载策略结果：");LO
        LO_result_data=object_result_data(x,task,execution_time,task_num,task_out,task_in);
        
        %% 基于云服务器的完全卸载（FOCPSO）
        [FOC,FOC_strategy]=FOCPSO(task,execution_time,task_num,task_out,task_in);
        disp("基于云服务器的完全卸载:");FOC
        FOC_result_data=object_result_data(FOC_strategy,task,execution_time,task_num,task_out,task_in);
        
        %% 基于端云的部分卸载（POPSO）
        [PO,PO_strategy]=POPSO(task,execution_time,task_num,task_out,task_in,LO,MaxNum);
        disp("基于端云的部分卸载:");PO
        PO_result_data=object_result_data(PO_strategy,task,execution_time,task_num,task_out,task_in);
        
        %% 端-云-边多重资源卸载策略（MRPSO）
        [MR,MR_strategy] = MRPSO(task,execution_time,task_num,task_out,task_in,LO,MaxNum);
        disp("端-云-边卸载策略:");MR
        MR_result_data=object_result_data(MR_strategy,task,execution_time,task_num,task_out,task_in);
        
        %% 结果存储
        result(i,:)=[LO FOC PO MR];
        purchase_cost(i,:)=[LO_result_data(1) FOC_result_data(1) PO_result_data(1) MR_result_data(1)];
        completion_time(i,:)=[LO_result_data(2) FOC_result_data(2) PO_result_data(2) MR_result_data(2)];
        Power_Cost(i,:)=[LO_result_data(3) FOC_result_data(3) PO_result_data(3) MR_result_data(3)];
        
        %% 卸载方案存储
        lables=["LOPSO";"FOCPSO";"POPSO";"MRPSO"];
        strategy(1:4,:)=[zeros(1, narvs);FOC_strategy;PO_strategy;MR_strategy];
        strategy=[lables,strategy];
        strategy_name=lab(i);
        xlswrite('./'+file_name+'/'+strategy_name+'.xls',strategy);
        clear strategy;
    end
    
    %% 适应度值对比图
    figure();
    b=bar(result);
    grid on;
    xlabel('DNNs')
    legend('LOPSO','FOCPSO','POPSO','MRPSO')
    set(gca,'XTickLabel',{'vgg11','vgg13','vgg16','vgg19','googlenet','darknet53','fuse'})
    title("适应度值对比图");
    
    %% 云配置价格、执行时间、能耗对比图
    figure();
    b=bar(purchase_cost);
    grid on;
    xlabel('DNNs')
    legend('LOPSO','FOCPSO','POPSO','MRPSO')
    set(gca,'XTickLabel',{'vgg11','vgg13','vgg16','vgg19','googlenet','darknet53','fuse'})
    title("云配置价格对比图");
    
    figure();
    b=bar(completion_time);
    grid on;
    xlabel('DNNs')
    legend('LOPSO','FOCPSO','POPSO','MRPSO')
    set(gca,'XTickLabel',{'vgg11','vgg13','vgg16','vgg19','googlenet','darknet53','fuse'})
    title("执行时间对比图");
    
    figure();
    b=bar(Power_Cost);
    grid on;
    xlabel('DNNs')
    legend('LOPSO','FOCPSO','POPSO','MRPSO')
    set(gca,'XTickLabel',{'vgg11','vgg13','vgg16','vgg19','googlenet','darknet53','fuse'})
    title("能耗对比图");
    
    
    figure();
    b=bar(floot_num);
    grid on;
    xlabel('DNNs')
    set(gca,'XTickLabel',{'vgg11','vgg13','vgg16','vgg19','googlenet','darknet53','fuse'})
    title("浮点运算次数");
    
    
    %% 数据存储
    
    lables1=[" ";"vgg11";"vgg13";"vgg16";"vgg19";"googlenet";"darknet53"];
    lables2=["LOPSO","FOCPSO","POPSO","MRPSO"];
    
    completion_time=[lables2;completion_time];
    completion_time=[lables1,completion_time];
    xlswrite('./'+file_name+'/completion_time.xls',completion_time);
    
    purchase_cost=[lables2;purchase_cost];
    purchase_cost=[lables1,purchase_cost];
    xlswrite('./'+file_name+'/purchase_cost.xls',purchase_cost);
    
    Power_Cost=[lables2;Power_Cost];
    Power_Cost=[lables1,Power_Cost];
    xlswrite('./'+file_name+'/Power_Cost.xls',Power_Cost);
    
    result=[lables2;result];
    result=[lables1,result];
    xlswrite('./'+file_name+'/result.xls',result);
    
    %% 清空环境
    clc
    clear
    close all
end